package other.xsy.paas;

import com.rkhd.platform.sdk.data.model.Order;
import com.rkhd.platform.sdk.data.model.OrderProduct;
import com.rkhd.platform.sdk.exception.ApiEntityServiceException;
import com.rkhd.platform.sdk.exception.ScriptBusinessException;
import com.rkhd.platform.sdk.log.Logger;
import com.rkhd.platform.sdk.log.LoggerFactory;
import com.rkhd.platform.sdk.model.XObject;
import com.rkhd.platform.sdk.trigger.DataResult;
import com.rkhd.platform.sdk.trigger.Trigger;
import com.rkhd.platform.sdk.trigger.TriggerRequest;
import com.rkhd.platform.sdk.trigger.TriggerResponse;
import other.xsy.paas.utils.HCXObjectService;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 功能描述：订单明细添加、修改、删除时，汇总非服务产品或赠品数量
 *
 * @author whz
 * @ClassName: OrderProductTrigger
 * @Description:
 * @date 2023年05月29日
 */
public class OrderProductTrigger implements Trigger {

    private static final Logger logger = LoggerFactory.getLogger();
    private static HCXObjectService hcxObjectService = new HCXObjectService();

    public static void main(String[] args) throws ApiEntityServiceException {
        OrderProductTrigger test = new OrderProductTrigger();
        OrderProduct orderProduct = new OrderProduct();
        orderProduct.setId(3113249153375075L);
        orderProduct = hcxObjectService.get(orderProduct);
        test.handler(Arrays.asList(orderProduct));
    }

    @Override
    public TriggerResponse execute(TriggerRequest request) throws ScriptBusinessException {
        List<XObject> dataList = request.getDataList();
        List<DataResult> dataResultList = new ArrayList<>();
        List<OrderProduct> orderProductList = new ArrayList<>();
        for (XObject xObject : dataList) {
            OrderProduct orderProduct = (OrderProduct) xObject;
            orderProductList.add(orderProduct);
            dataResultList.add(new DataResult(true, "", xObject));
        }
        handler(orderProductList);
        return new TriggerResponse(true, "", dataResultList);
    }

    private void handler(List<OrderProduct> orderProductList) {
        try {
            logger.info("订单明细添加、修改、删除时，汇总非服务产品或赠品数量");
            OrderProduct orderProduct = orderProductList.get(0);
            Long orderId = orderProduct.getOrderId();
            Order order = new Order();
            order.setId(orderId);
            order = hcxObjectService.get(order);
            Long entityType = order.getEntityType();
            logger.info("订单编号：" + order.getPo());
            logger.info("entityType:" + entityType);
        } catch (Exception e) {
            logger.error("系统异常：" + e.getMessage());
        }
    }
}
